package com.zd.server;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Server01 {

    public static void main(String[] args) throws IOException {

        // 1 创建服务器 , 需要指定端口号
        ServerSocket serverSocket =
                new ServerSocket(8888);

        // 2 启动服务器 监听客户端的连接  这样代码会线程阻塞 等，如果么有客户端的连接，一直等
        Socket socket = serverSocket.accept();

        System.out.println("服务器启动成功 , 开始监听端口 , 等待客户端的连接");


        BufferedReader bufferedReader =
                new BufferedReader(new InputStreamReader(socket.getInputStream()));

        String name = bufferedReader.readLine();

        System.out.println("服务器收到客户端发送的数据是 : " + name);

        PrintStream printStream = new PrintStream(socket.getOutputStream());
        printStream.println("hello: " +name);
        printStream.flush();



    }
}
